{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import collections\n",
    "import json\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rhythms_per_patient(data_json):\n",
    "    rhythms_patients = collections.defaultdict(set)\n",
    "    with open(data_json, 'r') as fid:\n",
    "        examples = [json.loads(l) for l in fid]\n",
    "    for ex in examples:\n",
    "        pid = os.path.basename(ex['ecg']).split(\"_\")[0]\n",
    "        for l in ex['labels']:\n",
    "            rhythms_patients[l].add(pid)\n",
    "    rs = sorted(rhythms_patients.keys())\n",
    "    for r in rs:\n",
    "        print \"{:>10} {}\".format(r, len(rhythms_patients[r]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training set\n",
      "        AF 9313\n",
      "       AVB 3043\n",
      "  BIGEMINY 3181\n",
      "       EAR 4367\n",
      "       IVR 2572\n",
      "JUNCTIONAL 2484\n",
      "     NOISE 10751\n",
      "     SINUS 33540\n",
      "       SVT 8228\n",
      " TRIGEMINY 3119\n",
      "        VT 5722\n",
      "WENCKEBACH 2395\n",
      "Testing set\n",
      "        AF 59\n",
      "       AVB 48\n",
      "  BIGEMINY 22\n",
      "       EAR 22\n",
      "       IVR 34\n",
      "JUNCTIONAL 36\n",
      "     NOISE 40\n",
      "     SINUS 213\n",
      "       SVT 34\n",
      " TRIGEMINY 20\n",
      "        VT 17\n",
      "WENCKEBACH 29\n"
     ]
    }
   ],
   "source": [
    "print \"Training set\"\n",
    "rhythms_per_patient(\"../train.json\")\n",
    "print \"Testing set\"\n",
    "rhythms_per_patient(\"../test.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
